package com.luber.backstage.service.impl;

import java.util.Date;
import java.util.List;

import com.luber.backstage.domain.EticketRoute;
import com.luber.backstage.domain.EticketStation;
import com.luber.backstage.domain.vo.*;
import com.luber.backstage.mapper.EticketRouteMapper;
import com.luber.backstage.mapper.EticketStationMapper;
import com.luber.backstage.service.IEticketRouteService;
import com.luber.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author luber
 * @date 2021-01-31
 */
@Service
public class EticketRouteServiceImpl implements IEticketRouteService
{
    @Autowired
    private EticketRouteMapper eticketRouteMapper;
    @Autowired
    private EticketStationMapper eticketStationMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public EticketRouteVO selectEticketRouteById(Long id)
    {
        return eticketRouteMapper.selectEticketRouteById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param eticketRoute 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<EticketRouteVO> selectEticketRouteList(EticketRoute eticketRoute)
    {
        List<EticketRouteVO> eticketRouteVOS = eticketRouteMapper.selectEticketRouteList(eticketRoute);
        for (EticketRouteVO eticketRouteVO : eticketRouteVOS) {
            eticketRouteVO.setStations(eticketStationMapper.getEticketStationByRouteId(eticketRouteVO.getId()));
        }
        return eticketRouteVOS;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param vo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public String updateEticketRoute(UpdateEticketRouteVO vo)
    {
        try {
            EticketRoute eticketRoute = new EticketRoute();
            BeanUtils.copyBeanProp(eticketRoute,vo);
            eticketRouteMapper.updateEticketRoute(eticketRoute);
            List<EticketStationVO> stations = vo.getStations();
            Long routeId = eticketRoute.getId();
            List<EticketStationVO> eticketStationByRouteId = eticketStationMapper.getEticketStationByRouteId(routeId);
            for (EticketStationVO eticketStationVO : eticketStationByRouteId) {
                eticketStationMapper.deleteEticketStationById(eticketStationVO.getId());
            }
            for (EticketStationVO station : stations) {
                EticketStation eticketStation = new EticketStation();
                BeanUtils.copyBeanProp(eticketStation,station);
                eticketStationMapper.insertEticketStation(eticketStation);
            }
            return "更新成功";
        }catch (Exception e){
            return "更新失败";
        }
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteEticketRouteByIds(Long[] ids)
    {
        eticketStationMapper.deleteEticketStationByRouteIds(ids);
        return eticketRouteMapper.deleteEticketRouteByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteEticketRouteById(Long id)
    {

        eticketStationMapper.deleteEticketStationByRouteId(id);
        return eticketRouteMapper.deleteEticketRouteById(id);
    }

    @Override
    public String add(AddEticketRouteVO vo) {
        try {
            EticketRoute eticketRoute = new EticketRoute();
            eticketRoute.setRouteName(vo.getRouteName());
            eticketRoute.setCreateUser(vo.getCreateUser());
            eticketRoute.setCreateDate(new Date().getTime());
            eticketRouteMapper.insertEticketRoute(eticketRoute);
            EticketRouteVO maxCreateDateEticketRoute = eticketRouteMapper.getMaxCreateDateEticketRoute();
            EticketStation eticketStation = new EticketStation();
            if (vo.getStations() != null){
                List<AddEticketStationVO> stations = vo.getStations();
                if (stations.size() > 0){
                    int i = 0;
                    for (AddEticketStationVO station : stations) {
                        eticketStation.setRouteId(maxCreateDateEticketRoute.getId());
                        eticketStation.setStationName(station.getStationName());
                        eticketStation.setStationNum(++i);
                        eticketStationMapper.insertEticketStation(eticketStation);
                    }
                }
            }
            return "新增成功";
        }catch (Exception e){
            return "未知错误";
        }
    }
}
